package com.zscg.sequence.service;

import java.io.IOException;
import java.io.OutputStream;

import android.content.Context;

import com.zgcs.sequence.net.out.OutPackage;
import com.zgcs.sequence.tool.ServiceTool;
import com.zscg.sequence.model.UserManager;

public class SequenceClinet {

	Context context;
	OutputStream out;

	public SequenceClinet(Context context, OutputStream out) {
		this.context = context;
		this.out = out;
	}

	private void sendPackage(OutPackage outPackage) {
		try {
			out.write(outPackage.packageToString().getBytes());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void regist(String email, String psw) {
		OutPackage regist = new OutPackage(ServiceTool.ORDER_UR);
		regist.addParameter(email);
		regist.addParameter(psw);
		sendPackage(regist);
	}

	public void login(String email, String psw) {

		OutPackage login = new OutPackage(ServiceTool.ORDER_UL);
		login.addParameter(email);
		login.addParameter(psw);
		sendPackage(login);
	}

	public void roomList(int userId, int startPosition, int count) {
		OutPackage roomList = new OutPackage(ServiceTool.ORDER_RI);
		roomList.addParameter(userId);
		roomList.addParameter(startPosition);
		if (count != 0)
			roomList.addParameter(count);
		else
			roomList.addParameter("");
		sendPackage(roomList);
	}

	public void joinRoom(int userId, int roomID) {
		OutPackage outPackage = new OutPackage(ServiceTool.ORDER_RGA);
		outPackage.addParameter(roomID);
		outPackage.addParameter(userId);
		sendPackage(outPackage);
	}

	/**
	 * 获取房价用户列表
	 * 
	 * @param roomID
	 * @param userOut
	 */
	public void getRoomUsers(int roomID, int userOut) {

		OutPackage outPackage = new OutPackage(ServiceTool.ORDER_RU);
		outPackage.addParameter(roomID);
		if (userOut != -1)
			outPackage.addParameter(userOut);
		else
			outPackage.addParameter("");
		sendPackage(outPackage);
	}

	/**
	 * 玩家自动退出房间
	 * 
	 * @param roomID
	 * @param userID
	 */
	public void exitRoomSelf(int roomID, int userID) {
		OutPackage outPackage = new OutPackage(ServiceTool.ORDER_RISO);
		outPackage.addParameter(roomID);
		outPackage.addParameter(userID);
		sendPackage(outPackage);

	}

	/**
	 * 开始游戏
	 * 
	 * @param roomID
	 * @param userID
	 */
	public void startGame(int roomID, int userID) {

		OutPackage outPackage = new OutPackage(ServiceTool.ORDER_RISG);
		outPackage.addParameter(roomID);
		outPackage.addParameter(userID);
		sendPackage(outPackage);
	}

	/**
	 * 接龙过程
	 * 
	 * @param roomID
	 *            房间ID
	 * @param preKey
	 *            上个成语的尾字
	 * @param nexKey
	 *            用户回复的成语
	 */
	public void idiomGame(int roomID, String preKey, String nexKey) {

		OutPackage outPackage = new OutPackage(ServiceTool.ORDER_RIG);
		outPackage.addParameter(roomID);
		outPackage
				.addParameter(UserManager.getInstance().getUser().getUserId());
		outPackage.addParameter(preKey);
		outPackage.addParameter(nexKey);
		sendPackage(outPackage);
	}

	/**
	 * 用户道具
	 * 
	 * @param roomID
	 *            房间ID
	 * @param toID
	 *            被嘘的对象
	 * @param style
	 *            1：嘘他 2：加油 3:赞一个 4：快一点吧
	 */
	public void idiomComment(int roomID, int toID, int style) {

		OutPackage outPackage = new OutPackage(ServiceTool.ORDER_RIC);
		outPackage.addParameter(roomID);
		outPackage
				.addParameter(UserManager.getInstance().getUser().getUserId());
		outPackage.addParameter(toID);
		outPackage.addParameter(style);
		sendPackage(outPackage);
	}

	/**
	 * 玩家回答超时
	 * 
	 * @param roomID
	 *            房间ID
	 */
	public void gameTimeout(int roomID) {
		OutPackage outPackage = new OutPackage(ServiceTool.ORDER_GT);
		outPackage.addParameter(roomID);
		outPackage
				.addParameter(UserManager.getInstance().getUser().getUserId());
		sendPackage(outPackage);
	}

	/**
	 * 使用道具
	 * 
	 * @param roomID
	 *            房間ID
	 * @param toUserID
	 *            被使用者ID
	 * @param commentStyle
	 *            使用類型 1：嘘他 2：加油 3:赞一个 4：快一点吧
	 */
	public void roomComment(int roomID, int toUserID, int commentStyle) {
		OutPackage outPackage = new OutPackage(ServiceTool.ORDER_RIC);
		outPackage.addParameter(roomID);
		outPackage
				.addParameter(UserManager.getInstance().getUser().getUserId());
		outPackage.addParameter(toUserID);
		outPackage.addParameter(commentStyle);
		sendPackage(outPackage);
	}

	public void getCurrentSequenceUser(int roomID) {
		OutPackage outPackage = new OutPackage(ServiceTool.ORDER_RICU);
		outPackage.addParameter(roomID);
		sendPackage(outPackage);
	}

}
